
package com.heimali.happyreader.ui.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heimali.happyreader.R;
import com.heimali.happyreader.config.ActionCode;
import com.heimali.happyreader.core.intf.IActionListener;
import com.heimali.happyreader.core.manager.GlobalManager;
import com.heimali.happyreader.utils.AndroidUtils;

public class LoginView extends LinearLayout {

    private TextView mUsernameTextView;
    private TextView mPasswordTextView;
    private Button mLoginButton;
    private ProgressDialog mLoginDialog;

    public LoginView(Context context, AttributeSet attrs) {
        super(context, attrs);
        GlobalManager.getInstance().userManager.registerActionListener(new UserListener());
        init(context);
    }

    private void init(Context context) {
        if (mLoginDialog == null) {
            mLoginDialog = new ProgressDialog(context);
            mLoginDialog.setCancelable(true);
            mLoginDialog.setIndeterminate(false);
            mLoginDialog.setMessage(context.getString(R.string.logining));
        }
        
        LayoutInflater inflater = LayoutInflater.from(context);
        LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.view_login, null);

        mUsernameTextView = (TextView) rootView.findViewById(R.id.username_edit);
        mPasswordTextView = (TextView) rootView.findViewById(R.id.password_edit);
        mLoginButton = (Button) rootView.findViewById(R.id.login_btn);
        mLoginButton.setOnClickListener(new ClickListener());
        addView(rootView, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    }

    private void login() {
        if (mLoginDialog != null && !mLoginDialog.isShowing()) {
            mLoginDialog.show();
        }
        
        String username = mUsernameTextView.getText().toString().trim();
        String password = mPasswordTextView.getText().toString().trim();

        if (TextUtils.isEmpty(username)) {
            return;
        }

        if (TextUtils.isEmpty(password)) {
            return;
        }

        GlobalManager.getInstance().userManager.asyncLogin(username, password);
    }

    class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.login_btn:
                    login();
                    break;
            }
        }
    }

    class UserListener implements IActionListener {
        @Override
        public Object actionPerformed(int actionCode, Object... args) {
            if (mLoginDialog != null && mLoginDialog.isShowing()) {
                mLoginDialog.dismiss();
            }
            
            switch (actionCode) {
                case ActionCode.LOGIN_FAILED:
                    AndroidUtils.toast(R.string.server_busy);
                    break;
                case ActionCode.LOGIN_PASSWORD_ERROR:
                    AndroidUtils.toast(R.string.username_or_password_error);
                    break;
            }
            return null;
        }
    }
}
